from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.meiduo_admin.serializers.orders import OrderInfoSerializer
from apps.meiduo_admin.utils import PageNum
from apps.orders.models import OrderInfo


class OrderInfoView(ModelViewSet):

    queryset = OrderInfo.objects.all()
    serializer_class = OrderInfoSerializer
    pagination_class = PageNum


    @action(methods=['PUT'], detail=True)
    def status(self, request, pk):

        order = self.get_object()
        status = request.data.get('status')

        # "UNPAID": 1,
        # "UNSEND": 2,
        # "UNRECEIVED": 3,
        # "UNCOMMENT": 4,
        # "FINISHED": 5

        if status not in [OrderInfo.ORDER_STATUS_ENUM['UNPAID'],
                          OrderInfo.ORDER_STATUS_ENUM['UNSEND'],
                          OrderInfo.ORDER_STATUS_ENUM['UNRECEIVED'],
                          OrderInfo.ORDER_STATUS_ENUM['UNCOMMENT'],
                          OrderInfo.ORDER_STATUS_ENUM['FINISHED']]:
            return Response(status=400)

        order.status = status
        order.save()
        return Response({
            'order_id': order.order_id,
            'status': status
        })